﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject.Entity;

using FluentNHibernate.Mapping;

namespace SmartProject.Mapping
{
    /// <summary>
    /// Mapping to Task class.
    /// </summary>
    public class TaskMap : ClassMap<Task>
    {
        /// <summary>
        /// Creates a new instance of TaskMap class and setups the table.
        /// </summary>
        public TaskMap()
        {
            Table("SmartProject_Task");
            Id(x => x.Id).Column("TaskId");
            References<User>(x => x.Owner)
                .Column("OwnerId")
                .Fetch.Select();
            References<User>(x => x.Assignee)
                .Column("AssigneeId")
                .Nullable();
            References<Product>(x => x.Product)
                .Column("ProductId")
                .Fetch.Select();
            References<Sprint>(x => x.Sprint)
                .Column("SprintId")
                .Nullable();
            References<TaskStatus>(x => x.Status)
                .Column("StatusId")
                .Not.Nullable();
            References<Priority>(x => x.Priority)
                .Column("PriorityId")
                .Fetch.Select()
                .Not.Nullable();
            References<TaskCategory>(x => x.Category)
                .Column("TaskCategoryId")
                .Fetch.Select()
                .Not.Nullable();
            References<TimeType>(x => x.ActualTimeType)
                .Column("ActualTimeTypeId")
                .Fetch.Select()
                .Nullable();
            References<TimeType>(x => x.EstimatedTimeType)
                .Column("EstimatedTimeTypeId")
                .Fetch.Select()
                .Nullable();
            Map(x => x.ActualTime).Nullable();
            Map(x => x.EstimatedTime).Nullable();
            Map(x => x.Description).Length(300);
            Map(x => x.Started).Nullable();
            Map(x => x.DueDate).Nullable();
            Map(x => x.Completed).Nullable();
            Map(x => x.Assigned).Nullable();
            HasMany(x => x.Attachments)
                .KeyColumn("TaskId")
                .LazyLoad()
                .Cascade.Delete();
            HasMany<Note>(x => x.Notes)
                .KeyColumn("TaskId")
                .Cascade.All();
            Map(x => x.Active).Not.Nullable();
            Map(x => x.Created).Not.Nullable();
        }
    }
}
